package com.imy.luascript.app.ui.dialog.createproject;

import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.common.base.BaseDialog;
import com.imy.common.constant.Constant;
import com.imy.common.util.FileUtils;
import com.imy.common.util.ToastUtil;
import com.imy.common.util.UiUtil;
import com.imy.lua.dialog.FileNameDialog;
import com.imy.lua.helper.properties.PropertiesLoaderHelper;
import com.imy.lua.utils.ProjectUtils;
import com.imy.luascript.R;
import com.imy.luascript.event.ProjectEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class CreateProjectDialog extends BaseDialog {
    @BindView(R.id.create_mould_ll)
    LinearLayout createMouldLl;
    @BindView(R.id.create_dir_ll)
    LinearLayout createDirLl;
    @BindView(R.id.create_script_ll)
    LinearLayout createScriptLl;

    public CreateProjectDialog(AppCompatActivity activity) {
        super(activity);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_create_project;
    }

    @OnClick({R.id.create_mould_ll, R.id.create_dir_ll, R.id.create_script_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.create_mould_ll:
//                listener.onCreteMould();
                dismiss();
                FileNameDialog fileNameDialog = new FileNameDialog((AppCompatActivity) mActivity);
                fileNameDialog.getFileNameEt().setHint("请输入工程名");
                fileNameDialog.show("请命名工程名");
                fileNameDialog.setFileListener(fileName -> {
                    //从assets复制整个模板到SD卡
                    File file = new File(Constant.PROJECT_PATH + "/" + fileName);
                    if (file.exists()) {
                        ToastUtil.toast(mActivity, "该工程已经存在，请勿重复创建");
                        return;
                    }
                    boolean project = ProjectUtils.copyProjectToSD(mActivity, "project",
                            Constant.APP_NAME + "/" + fileName,fileName);
                    if (project ) {
                        ToastUtil.toast(mActivity, "创建模板成功");
                        EventBus.getDefault().post(ProjectEvent.create(ProjectEvent.CREATE_PROJECT));
                    }
                });
                break;
            case R.id.create_dir_ll:
                dismiss();
                fileNameDialog = new FileNameDialog((AppCompatActivity) mActivity);
                fileNameDialog.getFileNameEt().setHint("请输入文件夹名");
                fileNameDialog.show("请命名文件夹");
                fileNameDialog.setFileListener(fileName -> {
                    File file = new File(Constant.PROJECT_PATH + "/" + fileName);
                    if (file.exists()) {
                        ToastUtil.toast(mActivity, "该文件夹已经存在，请勿重复创建");
                        return;
                    }
                    file.mkdir();
                    EventBus.getDefault().post(ProjectEvent.create(ProjectEvent.REFRESH));
                });

                break;
            case R.id.create_script_ll:
                break;
        }
    }


}
